package com.trendytech.tds.common.filter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.trendytech.tds.entity.OperationLog;

public class LogRecrod implements HttpSessionBindingListener,Serializable  {

	private static final long serialVersionUID = -8957797256558029340L;
	private static final String RECROD_LOGIN ="0152e90a29c80001";
	private static final String RECROD_LOGOUT ="0152ece1a02d0001";
	private String userId;
	private String userName;
	private String ip;
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void valueBound(HttpSessionBindingEvent event) {
/*	    HttpSession session = event.getSession();  
	    ServletContext application = session.getServletContext();  

	    // 把用户名放入在线列表  
		List<String> onlineUserList = (List<String>) application.getAttribute("onlineUserList");  
	    // 第一次使用前，需要初始化  
	    if (onlineUserList == null) {  
	        onlineUserList = new ArrayList<String>();  
	        application.setAttribute("onlineUserList", onlineUserList);  
	    }  
	    onlineUserList.add(this.userName); 
	    
	    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
		IUserService userServiceBean = (IUserService)context.getBean("userService");
		com.trendytech.tds.entity.User user = userServiceBean.getByUsername(userName);
		if (user == null) {
			throw new UsernameNotFoundException(userName);
		}
		if (null != userName && !"".equals(userName)) {
			String userId = user.getId();
			OperationLog operationLog = new OperationLog(userId, RECROD_LOGIN);
			IOperationLogService operationLogServiceBean = (IOperationLogService)context.getBean("logService");
			operationLog.setIp(this.ip);
			//TODO 操作日志 写死
			operationLog.setResult(1);
			operationLogServiceBean.insert(operationLog);
		}*/

	}


	@SuppressWarnings("unchecked")
	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
/*	    HttpSession session = event.getSession();
	    ServletContext application = session.getServletContext();  
	  
	    // 从在线列表中删除用户名  
	    List<String> onlineUserList = (List<String>) application.getAttribute("onlineUserList");  
	    if (null != onlineUserList && null != userName && !onlineUserList.isEmpty()){
		    onlineUserList.remove(this.userName);  
		    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
			IUserService userServiceBean = (IUserService)context.getBean("userService");
			com.trendytech.tds.entity.User user = userServiceBean.getByUsername(userName);
			if (user == null) {
				throw new UsernameNotFoundException(userName);
			}
			if (null != userName && !"".equals(userName)) {

				String userId = user.getId();
				OperationLog operationLog = new OperationLog(userId, RECROD_LOGOUT);
				IOperationLogService operationLogServiceBean = (IOperationLogService)context.getBean("logService");
				operationLog.setIp(this.ip);
				operationLog.setResult(1);
				operationLogServiceBean.insert(operationLog);
			}
	    }*/
	}
}
